package com.study.cloud.apilog.response;

import com.study.cloud.apilog.GatewayApiLog;
import com.study.cloud.utils.MediaTypeUtil;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Date 2022/5/17
 * @Author Cliff
 * desc
 */
@Component
public class SimpleResponseBodyImpl extends AbstractResponseBody {


    @Override
    public boolean isSupport(MediaType mediaType) {
        return !(mediaType != null
                && (MediaTypeUtil.isFormDataType(mediaType)
                || MediaTypeUtil.isWWWFormType(mediaType)
                || MediaTypeUtil.isApplicationJsonType(mediaType)));

    }

    @Override
    public Mono<Void> handler(ServerWebExchange exchange, GatewayFilterChain chain, GatewayApiLog gatewayLog, Object request) {
        ServerHttpRequest mutableReq = exchange.getRequest().mutate().build();

        //获取响应体
        ServerHttpResponseDecorator decoratedResponse = recordResponseLog(exchange, gatewayLog);
        return getThen(exchange, chain, gatewayLog, mutableReq, decoratedResponse);
    }

}
